package com.bookstore.web.user.controller;

import com.bookstore.common.result.Result;
import com.bookstore.model.dto.LoginDTO;
import com.bookstore.model.dto.RegisterDTO;
import com.bookstore.model.vo.LoginVO;
import com.bookstore.model.vo.RegisterVO;
import com.bookstore.service.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/auth")
@Api(tags = "认证接口")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<LoginVO> login(@RequestBody @Validated LoginDTO loginDTO, HttpServletRequest request) {
        LoginVO loginVO = authService.login(loginDTO, request);
        return Result.success(loginVO);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result<RegisterVO> register(@RequestBody @Validated RegisterDTO registerDTO, HttpServletRequest request) {
        RegisterVO registerVO = authService.register(registerDTO, request);
        return Result.success(registerVO);
    }

    /**
     * 检查用户名是否存在
     */
    @GetMapping("/check-username")
    @ApiOperation("检查用户名是否存在")
    public Result<Boolean> checkUsername(@RequestParam String username) {
        boolean exists = authService.checkUsernameExists(username);
        return Result.success(exists);
    }

    /**
     * 检查手机号是否存在
     */
    @GetMapping("/check-phone")
    @ApiOperation("检查手机号是否存在")
    public Result<Boolean> checkPhone(@RequestParam String phone) {
        boolean exists = authService.checkPhoneExists(phone);
        return Result.success(exists);
    }

    /**
     * 检查邮箱是否存在
     */
    @GetMapping("/check-email")
    @ApiOperation("检查邮箱是否存在")
    public Result<Boolean> checkEmail(@RequestParam String email) {
        boolean exists = authService.checkEmailExists(email);
        return Result.success(exists);
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result<Void> logout(@RequestParam Long userId) {
        authService.logout(userId);
        return Result.success();
    }
}